const express = require('express');
const router = express.Router();

const goodsHandle = require('../router_handle/goods')
router.get('/list', goodsHandle.goodsList)

router.post('/delete/:id', goodsHandle.goodsDelete)

router.get('/category', goodsHandle.selectCategoryById)

router.post('/add', goodsHandle.goodsAdd)

router.post('/edit/:id', goodsHandle.goodsEdit)


// 上传图片接口
const fs = require('fs');
const multer = require('multer')

const storage = multer.diskStorage({
    destination: (req, file, cb) => {
        cb(null, './uploads')
    },
    filename: (req, file, cb) => {
        cb(null, Date.now() + file.originalname)
    }
})

const createFolder = folder => {
    try {
        fs.accessSync(folder)
    } catch (err) {
        fs.mkdirSync(folder)
    }
}

const uploadFolder = './uploads/'
createFolder(uploadFolder)
const upload = multer({
    storage
})

router.post('/upload', upload.single('file'), (req, res) => {
    const file = req.file
    // console.log('文件类型: %s', file.mimetype)
    // console.log('原始文件名: $s', file.originalname)
    // console.log('文件大小: %s', file.size)
    // console.log('文件保存路径: %s', file.path)
    res.json({
        res_code: 0,
        name: file.originalname,
        url: file.path
    })
})


module.exports = router